package com.service;
import static com.def.GenericDef.ACTION_FLG_ADD;
import static com.def.GenericDef.ACTION_FLG_DELETE;
import static com.def.GenericDef.ACTION_FLG_QUERY;
import static com.def.GenericDef.ACTION_FLG_UPDATE;
import static com.def.GenericDef.E_XPWOTOOL_NOT_FOUND_RECORDER;
import static com.def.GenericDef.E_XPWOTOOL_RECORDER_ALREADY_EXIST;
import static com.def.GenericDef.RETURN_CODE_OK;
import static com.def.GenericDef.RETURN_CODE_UNKNOWN;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.dao.IRetWoToolDao;
import com.icim.service.ICIMBaseService;
import com.model.Ret_wo_tool;
import com.tx.Xpwotool.XpwotoolI;
import com.tx.Xpwotool.XpwotoolIA;
import com.tx.Xpwotool.XpwotoolO;
import com.tx.Xpwotool.XpwotoolOA;
import com.util.GUIDGenerator;
import com.util.MqOpe;
import com.util.Util;

@Scope("prototype")
@Service("XpwotoolService")
public class XpwotoolService extends ICIMBaseService{
	private Logger logger = Logger.getLogger(XpwotoolService.class);
	@Autowired
	private XpwotoolI xpwotoolI;
	
	@Autowired
	private XpwotoolO xpwotoolO; 
	
	
	@Autowired
	private IRetWoToolDao retWoToolDao;
	
	@Autowired
	private MqOpe mqOpe;
	public String subMainProc(String strInMsg){
		logger.info("InTrx is "+ strInMsg );
		xpwotoolO.setTrx_id("XPWOTOOL");
		xpwotoolO.setType_id("O");
		try{
			xpwotoolI = (XpwotoolI) mqOpe.inStr2Obj(strInMsg,xpwotoolI);
			char action_flg = xpwotoolI.getAction_flg().charAt(0);
			switch( action_flg ){
				case ACTION_FLG_QUERY   :  queryByCondition()  ; break;   
				case ACTION_FLG_ADD     :  addFnc()            ; break;
				case ACTION_FLG_UPDATE  :  updateFnc()         ; break;
				case ACTION_FLG_DELETE  :  deleteFnc()         ; break;
				default                 :  errActionFnc();   
			}
			
		}catch(Exception ex){
			xpwotoolO.setRtn_code(RETURN_CODE_UNKNOWN);
			xpwotoolO.setRtn_code(ex.getMessage());
		}finally{
			logger.info(mqOpe.obj2Str(xpwotoolO));
		}
		return mqOpe.obj2Str(xpwotoolO);
	}
	public void addFnc(){
		List<XpwotoolOA> oaryList = new ArrayList<XpwotoolOA> ();
		
		XpwotoolIA iary = xpwotoolI.getIary().get(0);
		String bay_id = iary.getBay_id();
		String wo_id_fk = iary.getWo_id_fk();
		String ope_id_fk = iary.getOpe_id_fk();
		
		String hql ="FROM Ret_wo_tool where 1=1 ";
//		if( bay_id != null){
			hql = hql + " and bay_id ='" + bay_id + "'" ;
//		}
//		if( wo_id_fk !=null){
			hql = hql + " and wo_id_fk ='"+ wo_id_fk +"'" ;
//		}
//		if( ope_id_fk !=null){
			hql = hql + " and ope_id_fk ='" + ope_id_fk +"'" ;
//		}
		
		List<Ret_wo_tool> retWOToolList =retWoToolDao.find( hql );
		if(retWOToolList.size()>0){
			xpwotoolO.setRtn_code(E_XPWOTOOL_RECORDER_ALREADY_EXIST+"");
			xpwotoolO.setRtn_mesg("Recorder Arleady Exist");
			return ;
		}
		Ret_wo_tool ret_wo_tool = new Ret_wo_tool();
		ret_wo_tool.setEvt_seq_id(GUIDGenerator.javaGUID());
		ret_wo_tool.setBay_id(bay_id);
		ret_wo_tool.setWo_id_fk(wo_id_fk);
		ret_wo_tool.setOpe_id_fk(ope_id_fk);
		ret_wo_tool.setUnq_seq_id(GUIDGenerator.javaGUID());
		ret_wo_tool.setEff_flg(iary.getEff_flg());
		
		if( ("Y").equals(iary.getEff_flg()) ){
			ret_wo_tool.setEff_timestamp(Util.getCurrentTimestamp());
			ret_wo_tool.setValid_usr(iary.getInval_usr());
		}else{
			ret_wo_tool.setInval_usr(iary.getInval_usr());
		}
		ret_wo_tool.setEvt_timestamp(Util.getCurrentTimestamp());
		
		retWoToolDao.save(ret_wo_tool);
		
		xpwotoolO.setRtn_code(RETURN_CODE_OK);
		xpwotoolO.setRtn_mesg("success");
	}
	public void updateFnc(){ 
		XpwotoolIA iary = xpwotoolI.getIary().get(0);
		String bay_id = iary.getBay_id();
		String wo_id_fk = iary.getWo_id_fk();
		String ope_id_fk = iary.getOpe_id_fk();
		
		Ret_wo_tool retWoTool =retWoToolDao.getDataByKeyProc(bay_id,wo_id_fk,ope_id_fk);
		if( retWoTool != null){
			xpwotoolO.setRtn_code(E_XPWOTOOL_NOT_FOUND_RECORDER+"");
			xpwotoolO.setRtn_mesg("not found record");
			return ;
		}
		//获取主键
		retWoTool.setUnq_seq_id(GUIDGenerator.javaGUID());
		if(iary.getEff_flg()!=null){
			retWoTool.setEff_flg(iary.getEff_flg());
		}
		if(iary.getEff_timestamp()!=null){
			retWoTool.setEff_timestamp(iary.getEff_timestamp());
		}
		if(iary.getValid_usr()!=null){
			retWoTool.setValid_usr(iary.getValid_usr());
		}
		if(iary.getInval_usr()!=null){
			retWoTool.setInval_usr(iary.getInval_usr());
		}
		retWoTool.setEvt_timestamp(Util.getCurrentTimestamp());
		retWoToolDao.update(retWoTool);
        
        xpwotoolO.setRtn_code(RETURN_CODE_OK);
        xpwotoolO.setRtn_mesg("success");
	
	}
	public void deleteFnc(){
		
		XpwotoolIA iary = xpwotoolI.getIary().get(0);
		String bay_id = iary.getBay_id();
		String wo_id_fk = iary.getWo_id_fk();
		String ope_id_fk = iary.getOpe_id_fk();
		
		Ret_wo_tool retWoTool =retWoToolDao.getDataByKeyProc(bay_id,wo_id_fk,ope_id_fk);
		if( retWoTool != null){
			xpwotoolO.setRtn_code(E_XPWOTOOL_NOT_FOUND_RECORDER+"");
			xpwotoolO.setRtn_mesg("not found record");
			return ;
		}
		
		retWoToolDao.delete(retWoTool);
		
    	xpwotoolO.setRtn_code(RETURN_CODE_OK);
    	xpwotoolO.setRtn_mesg("success");
	}
	public void queryByCondition(){
		 /***
			 * 支持
			 *   
			 *   by bay_id,wo_id_fk,ope_id_fk 查询
			 *   
			 * **/
			 
			List<XpwotoolOA> oaryList = new ArrayList<XpwotoolOA> ();
			
			XpwotoolIA iary = xpwotoolI.getIary().get(0);
			String bay_id = iary.getBay_id();
			String wo_id_fk = iary.getWo_id_fk();
			String ope_id_fk = iary.getOpe_id_fk();
			
			String hql ="FROM Ret_wo_tool where 1=1 ";
			if( bay_id != null){
				hql = hql + " and bay_id ='" + bay_id + "'" ;
			}
			if( wo_id_fk !=null){
				if( wo_id_fk.indexOf('*')==-1){
					hql = hql + " and wo_id_fk='" + wo_id_fk + "'";
				}else{
					hql = hql + " and wo_id_fk like '" + wo_id_fk.replace('*', '%') + "'";
				}
				hql = hql + " and wo_id_fk ='"+ wo_id_fk +"'" ;
				
			}
			if( ope_id_fk !=null){
				hql = hql + " and ope_id_fk ='" + ope_id_fk +"'" ;
			}
			
			List<Ret_wo_tool> retWOToolList =retWoToolDao.find( hql );
			for(Ret_wo_tool ret_wo_tool : retWOToolList){
				XpwotoolOA oary = new XpwotoolOA();
				oary.setOpe_id_fk(ret_wo_tool.getOpe_id_fk());
				oary.setBay_id(ret_wo_tool.getBay_id());
				oary.setWo_id_fk(ret_wo_tool.getWo_id_fk());
				oary.setEff_flg(ret_wo_tool.getEff_flg());
				oary.setEff_timestamp(ret_wo_tool.getEff_timestamp());
				oary.setValid_usr(ret_wo_tool.getValid_usr());
				oary.setInval_usr(ret_wo_tool.getInval_usr());
				oary.setEvt_timestamp(ret_wo_tool.getEvt_timestamp());
				oaryList.add(oary);
			}
			xpwotoolO.setOary(oaryList);
			xpwotoolO.setTbl_cnt(oaryList.size());
			xpwotoolO.setRtn_code(RETURN_CODE_OK);
			xpwotoolO.setRtn_mesg("success");
	}
	public void errActionFnc(){
		xpwotoolO.setRtn_code("0000009");
		xpwotoolO.setRtn_mesg("Invalid Action_flg");
	}
	
}
